<?php

namespace app\controller;

use app\logic\LoginLogic;
use common\util\CookieUtil;
use common\validate\LoginValidate;
use think\App;
use think\Response;
use think\response\View;

class Login extends Base
{

    /**
     * @var LoginLogic $loginLogic
     */
    private LoginLogic $loginLogic;

    public function __construct(App $app, LoginLogic $loginLogic)
    {
        parent::__construct($app);
        $this->loginLogic = $loginLogic;
    }

    /**
     * 登录页
     *
     * @return View
     */
    public function index(): View
    {
        return view('login', [
            'title'         => $this->app_name . ' - 登录',
            'admin_user'    => CookieUtil::cookie('admin_user'),
            'admin_pass'    => CookieUtil::cookie('admin_pass'),
            'remember_pass' => CookieUtil::cookie('remember_pass'),
        ]);
    }

    /**
     * 登录
     *
     * @return Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function doLogin(): Response
    {
        $params = $this->request->post();
        $params['come_from'] = $this->request->post('come_from', 'pc');
        validate(LoginValidate::class)->check($params);
        $this->loginLogic->login($params);
        return $this->success([], '登录成功');
    }

    /**
     * 登出
     *
     * @return Response
     */
    public function logout(): Response
    {
        $this->loginLogic->logout();
        return $this->success([], '退出登录成功');
    }
}